package com.xayoudao.uneng.api;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xayoudao.uneng.util.HttpClientUtils;
import com.xayoudao.uneng.util.ServicesHolder;

/**
 * 和用户有关的api
 * @author junjun
 * 
 */
public class APIManager {
	// 采用单例模式（懒汉式） 减少对象的创建 三步可创建一个单例

	// 1.静态对象
	private static APIManager instance;

	// 2.构造方法私有化，防止外部实例化
	private APIManager() {
	};

	/**
	 * 3.静态工厂方法返回对像的实例
	 * 
	 * @return
	 */
	public static APIManager getInstance() {
		if (instance == null)
			instance = new APIManager();
		return instance;
	}
	
	/**
	 * 获得某用户的、某项活动的奖券列表
	 * @param context
	 * @param userid  用户ID
	 * @param eventid 活动ID
	 * @param handler
	 */
	public void subarea(Context context,String lat ,String lng , AsyncHttpResponseHandler handler){
		
		RequestParams params = new RequestParams() ;
		params.put("lat", lat) ;
		params.put("lng", lng) ;
		Log.d("API", ServicesHolder.api(ServicesHolder.SUBAREA));
		HttpClientUtils.get(context,ServicesHolder.api(ServicesHolder.SUBAREA), params,  handler) ;
	}
	
	/**
	 * 注册 api
	 * @param context
	 * @param mobile
	 * @param username
	 * @param udid
	 * @param handler
	 */
	public void register(Context context,String mobile,String username,String udid,AsyncHttpResponseHandler handler){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("User[mobile]", mobile) ;
		params.put("User[username]", username) ;
		
		params.put("User[password]", udid) ;
		HttpClientUtils.post(ServicesHolder.api(ServicesHolder.SUBAREA), params, context, handler) ;
		
	}
	/**
	 * 获得所有的活动列表 api
	 * @param context
	 * @param handler
	 */
	public void getEvent(Context context,AsyncHttpResponseHandler handler){
		HttpClientUtils.post(ServicesHolder.api(ServicesHolder.GET_EVENTS), null, context, handler) ;
	}
	/**
	 * 获得某用户的、某项活动的奖券列表
	 * @param context
	 * @param userid  用户ID
	 * @param eventid 活动ID
	 * @param handler
	 */
	public void getTickets(Context context,String userid ,String eventid , AsyncHttpResponseHandler handler){
		
		RequestParams params = new RequestParams() ;
		params.put("userid", userid) ;
		params.put("eventid", eventid) ;
		
		HttpClientUtils.get(context,ServicesHolder.api(ServicesHolder.GET_TICKETS), params,  handler) ;
	}
	/**
	 * 上传一次摇奖信息
	 * @param context
	 * @param userid
	 * @param eventid
	 * @param code
	 * @param handler
	 */
	public void shakeToServer(Context context,String userid,String eventid,String ticcode ,String code ,AsyncHttpResponseHandler handler){
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("Ticket[userid]", userid) ;
		params.put("Ticket[eventid]", eventid) ;
		params.put("Ticket[code]", ticcode) ;
		params.put("code", code) ;
		HttpClientUtils.post(ServicesHolder.api(ServicesHolder.SHAKEINFO_TOSERVER), params, context, handler) ;
	}
	
	/**
	 * 提交问卷调查 api
	 * @param context
	 * @param userid
	 * @param eventid
	 * @param question
	 * @param ask
	 * @param handler
	 */
	public void submitVote(Context context,String userid,String eventid, String question,String ask,AsyncHttpResponseHandler handler){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("Vote[userid]", userid) ;
		params.put("Vote[eventid]", eventid) ;
		params.put("Vote[question]", question) ;
		params.put("Vote[ask]", ask) ;
		HttpClientUtils.post(ServicesHolder.api(ServicesHolder.SUBMIT_VOTE), params, context, handler) ;
	}
}
